import { ControlVOBase, CtrlServiceBase, IParam } from '@/core';
import { IHttpResponse } from '@/core/interface/modules/common';
import { deepCopy, HttpResponse } from '@/core/utils';
class FormService<T extends ControlVOBase> extends CtrlServiceBase<T> {
	/**
	 * 远端数据
	 *
	 * @private
	 * @type {IParam}
	 * @memberof FormService
	 */
	private remoteCopyData: IParam = {};

	/**
	 * @description 处理响应
	 * @private
	 * @param {string} action 行为名称
	 * @param {*} response 响应
	 * @param {boolean} [isCreate=false] 是否新建
	 * @memberof FormService
	 */
	handleResponse(response: IHttpResponse, opts: any = {}) {
		response.data = this.newControlVO(response.data);
		return response;
	}

	/**
	 * 查询数据
	 *
	 * @param {string} action 行为名称
	 * @param {IParam} [context={}] 上下文
	 * @param {IParam} [data={}] 请求数据
	 * @return {*}  {Promise<IHttpResponse>}
	 * @memberof FormService
	 */
	async get(
		action: string,
		context: IParam = {},
		data: IParam = {}
	): Promise<IHttpResponse> {
		let result: Promise<any> | null = null;
		const { data: Data, context: Context } = this.handleRequestData(
			context,
			data
		);
		try {
			await this.initEntityService();
			result = this.appEntityService[action ? action : 'Get'](Context, Data);
			if (result) {
				const response = await result;
				this.setRemoteCopyData(response);
				this.handleResponse(response);
			}
		} catch (error) {}
		return result;
	}

	/**
	 * 加载草稿
	 *
	 * @param {string} action 行为名称
	 * @param {IParam} [context={}] 上下文
	 * @param {IParam} [data={}] 请求数据
	 * @return {*}  {Promise<IHttpResponse>}
	 * @memberof FormService
	 */
	async loadDraft(
		action: string,
		context: IParam = {},
		data: IParam = {}
	): Promise<IHttpResponse> {
		let result: Promise<any> | null = null;
		const { data: Data, context: Context } = this.handleRequestData(
			context,
			data
		);
		try {
			await this.initEntityService();
			result = this.appEntityService[action ? action : 'GetDraft'](
				Context,
				Data
			);
			if (result) {
				const response = await result;
				this.setRemoteCopyData(response);
				this.handleResponse(response);
			}
		} catch (error) {}
		return result;
	}

	/**
	 * 删除
	 *
	 * @param {string} action 行为名称
	 * @param {IParam} [context={}] 上下文
	 * @param {IParam} [data={}] 请求数据
	 * @return {*}  {Promise<IHttpResponse>}
	 * @memberof FormService
	 */
	async delete(
		action: string,
		context: IParam = {},
		data: IParam = {}
	): Promise<IHttpResponse> {
		let result: Promise<any> | null = null;
		const { data: Data, context: Context } = this.handleRequestData(
			context,
			data
		);
		try {
			await this.initEntityService();
			result = this.appEntityService[action ? action : 'Remove'](Context, Data);
			if (result) {
				const response = await result;
				this.handleResponse(response);
			}
		} catch (error) {}
		return result;
	}

	/**
	 * 更新
	 *
	 * @param {string} action 行为名称
	 * @param {IParam} [context={}] 上下文
	 * @param {IParam} [data={}] 请求数据
	 * @return {*}  {Promise<IHttpResponse>}
	 * @memberof FormService
	 */
	async update(
		action: string,
		context: IParam = {},
		data: IParam = {}
	): Promise<IHttpResponse> {
		let result: Promise<any> | null = null;
		const { data: Data, context: Context } = this.handleRequestData(
			context,
			data
		);
		try {
			await this.initEntityService();
			result = this.appEntityService[action ? action : 'Update'](Context, Data);
			if (result) {
				const response = await result;
				this.handleResponse(response);
			}
		} catch (error) {}
		return result;
	}

	/**
	 * 添加数据
	 *
	 * @param {string} action 行为名称
	 * @param {IParam} [context={}] 上下文
	 * @param {IParam} [data={}] 请求数据
	 * @return {*}  {Promise<IHttpResponse>}
	 * @memberof FormService
	 */
	async create(
		action: string,
		context: IParam = {},
		data: IParam = {}
	): Promise<IHttpResponse> {
		let result: Promise<any> | null = null;
		const { data: Data, context: Context } = this.handleRequestData(
			context,
			data
		);
		try {
			await this.initEntityService();
			result = this.appEntityService[action ? action : 'Create'](Context, Data);
			if (result) {
				const response = await result;
				this.handleResponse(response);
			}
		} catch (error) {}
		return result;
	}

	/**
	 * 前端逻辑
	 *
	 * @param {string} action 行为名称
	 * @param {IParam} [context={}] 上下文
	 * @param {IParam} [data={}] 请求数据
	 * @return {*}  {Promise<IHttpResponse>}
	 * @memberof FormService
	 */
	async frontLogic(
		action: string,
		context: IParam = {},
		data: IParam = {}
	): Promise<IHttpResponse> {
		const { data: Data, context: Context } = this.handleRequestData(
			context,
			data
		);
		try {
			await this.initEntityService();
			const response = await this.appEntityService[action](Context, Data);
			this.handleResponse(response);
			return response;
		} catch (error) {
			return new HttpResponse(error, null, 500);
		}
	}

	/**
	 * 工作流提交
	 *
	 * @param {string} action
	 * @param {IParam} [context={}]
	 * @param {IParam} [data={}]
	 * @return {*}  {Promise<IHttpResponse>}
	 * @memberof FormService
	 */
	async wfSubmit(
		action: string,
		context: IParam = {},
		data: IParam = {}
	): Promise<IHttpResponse> {
		let result: Promise<any> | null = null;
		const { data: Data, context: Context } = this.handleRequestData(
			context,
			data
		);
		try {
			await this.initEntityService();
			result = this.appEntityService[action ? action : 'WFSubmit'](
				Context,
				Data
			);
			if (result) {
				const response = await result;
				this.handleResponse(response);
			}
		} catch (error) {}
		return result;
	}

	/**
	 * 设置远端数据
	 *
	 * @private
	 * @param {*} response
	 * @memberof FormService
	 */
	private setRemoteCopyData(response: IHttpResponse) {
		if (response.success) {
			this.remoteCopyData = deepCopy(response.data);
		}
	}

	/**
	 * 获取远端数据
	 *
	 * @return {*}
	 * @memberof FormService
	 */
	getRemoteCopyData(): IParam {
		return this.remoteCopyData;
	}

	/**
	 * 获取跨实体数据集合
	 *
	 * @param {string} serviceName 服务名称
	 * @param {string} interfaceName 接口名称
	 * @param {*} data 参数
	 * @param {string} [deKeyField] 应用实体主信息属性名称
	 * @param {string} [deName] 应用实体主键属性名称
	 * @returns {*}  {Promise<any[]>}
	 * @memberof FormService
	 */
	public getItems(
		serviceName: string,
		interfaceName: string,
		context: any = {},
		data: any,
		deKeyField?: string,
		deName?: string
	): Promise<any[]> {
		data.page = data.page ? data.page : 0;
		data.size = data.size ? data.size : 1000;
		return new Promise((resolve: any, reject: any) => {
			App.getDataService(serviceName, context)
				.then(async (dataService: any) => {
					if (dataService && dataService[interfaceName] instanceof Function) {
						const response = await this.doItems(
							dataService[interfaceName](context, data),
							`${deKeyField}`,
							`${deName}`,
							dataService.appEntityTextCodeName,
							dataService.appEntityKeyCodeName
						);
						resolve(response);
					}
				})
				.catch((error: any) => {
					reject([]);
				});
		});
	}

	/**
	 * 处理数据
	 *
	 * @private
	 * @param {Promise<any>} promise
	 * @returns {Promise<any>}
	 * @memberof AppFormService
	 */
	private doItems(
		promise: Promise<any>,
		deKeyField: string,
		deName: string,
		textCodeName?: string,
		keyCodeName?: string
	): Promise<any> {
		return new Promise((resolve, reject) => {
			promise
				.then((response: any) => {
					if (response.success) {
						const data = response.data;
						data.forEach((item: any, index: number) => {
							item[deName] = item[deKeyField];
							if (textCodeName) {
								item.label = item[textCodeName];
							}
							if (keyCodeName) {
								item.value = item[keyCodeName];
							}
							data[index] = item;
						});
						resolve(data);
					} else {
						reject([]);
					}
				})
				.catch((response: any) => {
					reject([]);
				});
		});
	}
}
export default FormService;
